import requests
import time
from bs4 import BeautifulSoup

def get_text(a_url):
    url =a_url
    html = requests.get(url=url)
    html.encoding = 'gbk'
    soup = BeautifulSoup(html.text, 'lxml')

    find_title = soup.find('div', id="chapter_title")
    Title = find_title.h1.text

    find_text = soup.find('div', id="text_area")
    Text = find_text.text

    print('已爬取：'+Title)
    return '\n'+Title + '\n' + Text

def write2txt(the_text):
    with open('完美世界.txt','a',encoding='utf-8') as f:
        f.write(the_text)

def main():
    #A=103 #第A章开始
    #B=125 #第B章结束（包含第B章）
    URL = 'https://www.tmetb.net/42/42318/'  # 目录页
    HTML = requests.get(url=URL)
    HTML.encoding = 'gbk'  # 转编码
    SOUP = BeautifulSoup(HTML.text, 'lxml')
    booklist = SOUP.select_one('.box-item').find_all('a')  # 目录
    #for i in booklist[(A-1):B]:
    for i in booklist:
        text=get_text(URL + i['href'])
        write2txt(text)
        time.sleep(2)
    print('已爬取完毕')

if __name__ == '__main__':
    main()